<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="发货日期" prop="shippingDate">
      <el-input v-model="dataForm.shippingDate" placeholder="发货日期"></el-input>
    </el-form-item>
    <el-form-item label="业务员" prop="salesman">
      <el-input v-model="dataForm.salesman" placeholder="业务员"></el-input>
    </el-form-item>
    <el-form-item label="客户名称" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="编号" prop="number">
      <el-input v-model="dataForm.number" placeholder="编号"></el-input>
    </el-form-item>
    <el-form-item label="产品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="quantity">
      <el-input v-model="dataForm.quantity" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="金额_开票" prop="invoiceAmount">
      <el-input v-model="dataForm.invoiceAmount" placeholder="金额_开票"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="region">
      <el-input v-model="dataForm.region" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="开票公司" prop="invoiceCompany">
      <el-input v-model="dataForm.invoiceCompany" placeholder="开票公司"></el-input>
    </el-form-item>
    <el-form-item label="结算价" prop="settlementPrice">
      <el-input v-model="dataForm.settlementPrice" placeholder="结算价"></el-input>
    </el-form-item>
    <el-form-item label="指导价" prop="guidePrice">
      <el-input v-model="dataForm.guidePrice" placeholder="指导价"></el-input>
    </el-form-item>
    <el-form-item label="提成" prop="commission">
      <el-input v-model="dataForm.commission" placeholder="提成"></el-input>
    </el-form-item>
    <el-form-item label="到期时间" prop="dueDate">
      <el-input v-model="dataForm.dueDate" placeholder="到期时间"></el-input>
    </el-form-item>
    <el-form-item label="超期天数" prop="overdueDays">
      <el-input v-model="dataForm.overdueDays" placeholder="超期天数"></el-input>
    </el-form-item>
    <el-form-item label="回款银行" prop="bank">
      <el-input v-model="dataForm.bank" placeholder="回款银行"></el-input>
    </el-form-item>
    <el-form-item label="回款日期" prop="paymentDate">
      <el-input v-model="dataForm.paymentDate" placeholder="回款日期"></el-input>
    </el-form-item>
    <el-form-item label="对公回款金额" prop="publicPaymentAmount">
      <el-input v-model="dataForm.publicPaymentAmount" placeholder="对公回款金额"></el-input>
    </el-form-item>
    <el-form-item label="单位应收款" prop="unitReceivable">
      <el-input v-model="dataForm.unitReceivable" placeholder="单位应收款"></el-input>
    </el-form-item>
    <el-form-item label="业务员回款日期" prop="salesmanPaymentDate">
      <el-input v-model="dataForm.salesmanPaymentDate" placeholder="业务员回款日期"></el-input>
    </el-form-item>
    <el-form-item label="个人其他" prop="personalOthers">
      <el-input v-model="dataForm.personalOthers" placeholder="个人其他"></el-input>
    </el-form-item>
    <el-form-item label="业务员回款" prop="salesmanPayment">
      <el-input v-model="dataForm.salesmanPayment" placeholder="业务员回款"></el-input>
    </el-form-item>
    <el-form-item label="业务员应收" prop="salesmanReceivable">
      <el-input v-model="dataForm.salesmanReceivable" placeholder="业务员应收"></el-input>
    </el-form-item>
    <el-form-item label="业务员代垫" prop="salesmanAdvance">
      <el-input v-model="dataForm.salesmanAdvance" placeholder="业务员代垫"></el-input>
    </el-form-item>
    <el-form-item label="冲账金额" prop="chargeAmount">
      <el-input v-model="dataForm.chargeAmount" placeholder="冲账金额"></el-input>
    </el-form-item>
    <el-form-item label="代垫日期" prop="advanceDate">
      <el-input v-model="dataForm.advanceDate" placeholder="代垫日期"></el-input>
    </el-form-item>
    <el-form-item label="汇率" prop="exchangeRate">
      <el-input v-model="dataForm.exchangeRate" placeholder="汇率"></el-input>
    </el-form-item>
    <el-form-item label="汇兑" prop="exchange">
      <el-input v-model="dataForm.exchange" placeholder="汇兑"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          shippingDate: '',
          salesman: '',
          customerName: '',
          number: '',
          productName: '',
          quantity: '',
          unitPrice: '',
          invoiceAmount: '',
          region: '',
          invoiceCompany: '',
          settlementPrice: '',
          guidePrice: '',
          commission: '',
          dueDate: '',
          overdueDays: '',
          bank: '',
          paymentDate: '',
          publicPaymentAmount: '',
          unitReceivable: '',
          salesmanPaymentDate: '',
          personalOthers: '',
          salesmanPayment: '',
          salesmanReceivable: '',
          salesmanAdvance: '',
          chargeAmount: '',
          advanceDate: '',
          exchangeRate: '',
          exchange: '',
          remark: ''
        },
        dataRule: {
          shippingDate: [
            { required: true, message: '发货日期不能为空', trigger: 'blur' }
          ],
          salesman: [
            { required: true, message: '业务员不能为空', trigger: 'blur' }
          ],
          customerName: [
            { required: true, message: '客户名称不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '编号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          quantity: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          invoiceAmount: [
            { required: true, message: '金额_开票不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          invoiceCompany: [
            { required: true, message: '开票公司不能为空', trigger: 'blur' }
          ],
          settlementPrice: [
            { required: true, message: '结算价不能为空', trigger: 'blur' }
          ],
          guidePrice: [
            { required: true, message: '指导价不能为空', trigger: 'blur' }
          ],
          commission: [
            { required: true, message: '提成不能为空', trigger: 'blur' }
          ],
          dueDate: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          overdueDays: [
            { required: true, message: '超期天数不能为空', trigger: 'blur' }
          ],
          bank: [
            { required: true, message: '回款银行不能为空', trigger: 'blur' }
          ],
          paymentDate: [
            { required: true, message: '回款日期不能为空', trigger: 'blur' }
          ],
          publicPaymentAmount: [
            { required: true, message: '对公回款金额不能为空', trigger: 'blur' }
          ],
          unitReceivable: [
            { required: true, message: '单位应收款不能为空', trigger: 'blur' }
          ],
          salesmanPaymentDate: [
            { required: true, message: '业务员回款日期不能为空', trigger: 'blur' }
          ],
          personalOthers: [
            { required: true, message: '个人其他不能为空', trigger: 'blur' }
          ],
          salesmanPayment: [
            { required: true, message: '业务员回款不能为空', trigger: 'blur' }
          ],
          salesmanReceivable: [
            { required: true, message: '业务员应收不能为空', trigger: 'blur' }
          ],
          salesmanAdvance: [
            { required: true, message: '业务员代垫不能为空', trigger: 'blur' }
          ],
          chargeAmount: [
            { required: true, message: '冲账金额不能为空', trigger: 'blur' }
          ],
          advanceDate: [
            { required: true, message: '代垫日期不能为空', trigger: 'blur' }
          ],
          exchangeRate: [
            { required: true, message: '汇率不能为空', trigger: 'blur' }
          ],
          exchange: [
            { required: true, message: '汇兑不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cw/receivablesummary/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shippingDate = data.receivableSummary.shippingDate
                this.dataForm.salesman = data.receivableSummary.salesman
                this.dataForm.customerName = data.receivableSummary.customerName
                this.dataForm.number = data.receivableSummary.number
                this.dataForm.productName = data.receivableSummary.productName
                this.dataForm.quantity = data.receivableSummary.quantity
                this.dataForm.unitPrice = data.receivableSummary.unitPrice
                this.dataForm.invoiceAmount = data.receivableSummary.invoiceAmount
                this.dataForm.region = data.receivableSummary.region
                this.dataForm.invoiceCompany = data.receivableSummary.invoiceCompany
                this.dataForm.settlementPrice = data.receivableSummary.settlementPrice
                this.dataForm.guidePrice = data.receivableSummary.guidePrice
                this.dataForm.commission = data.receivableSummary.commission
                this.dataForm.dueDate = data.receivableSummary.dueDate
                this.dataForm.overdueDays = data.receivableSummary.overdueDays
                this.dataForm.bank = data.receivableSummary.bank
                this.dataForm.paymentDate = data.receivableSummary.paymentDate
                this.dataForm.publicPaymentAmount = data.receivableSummary.publicPaymentAmount
                this.dataForm.unitReceivable = data.receivableSummary.unitReceivable
                this.dataForm.salesmanPaymentDate = data.receivableSummary.salesmanPaymentDate
                this.dataForm.personalOthers = data.receivableSummary.personalOthers
                this.dataForm.salesmanPayment = data.receivableSummary.salesmanPayment
                this.dataForm.salesmanReceivable = data.receivableSummary.salesmanReceivable
                this.dataForm.salesmanAdvance = data.receivableSummary.salesmanAdvance
                this.dataForm.chargeAmount = data.receivableSummary.chargeAmount
                this.dataForm.advanceDate = data.receivableSummary.advanceDate
                this.dataForm.exchangeRate = data.receivableSummary.exchangeRate
                this.dataForm.exchange = data.receivableSummary.exchange
                this.dataForm.remark = data.receivableSummary.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cw/receivablesummary/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shippingDate': this.dataForm.shippingDate,
                'salesman': this.dataForm.salesman,
                'customerName': this.dataForm.customerName,
                'number': this.dataForm.number,
                'productName': this.dataForm.productName,
                'quantity': this.dataForm.quantity,
                'unitPrice': this.dataForm.unitPrice,
                'invoiceAmount': this.dataForm.invoiceAmount,
                'region': this.dataForm.region,
                'invoiceCompany': this.dataForm.invoiceCompany,
                'settlementPrice': this.dataForm.settlementPrice,
                'guidePrice': this.dataForm.guidePrice,
                'commission': this.dataForm.commission,
                'dueDate': this.dataForm.dueDate,
                'overdueDays': this.dataForm.overdueDays,
                'bank': this.dataForm.bank,
                'paymentDate': this.dataForm.paymentDate,
                'publicPaymentAmount': this.dataForm.publicPaymentAmount,
                'unitReceivable': this.dataForm.unitReceivable,
                'salesmanPaymentDate': this.dataForm.salesmanPaymentDate,
                'personalOthers': this.dataForm.personalOthers,
                'salesmanPayment': this.dataForm.salesmanPayment,
                'salesmanReceivable': this.dataForm.salesmanReceivable,
                'salesmanAdvance': this.dataForm.salesmanAdvance,
                'chargeAmount': this.dataForm.chargeAmount,
                'advanceDate': this.dataForm.advanceDate,
                'exchangeRate': this.dataForm.exchangeRate,
                'exchange': this.dataForm.exchange,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
